package dto

import (
	"time"

	"wxpay/pkg/util"

	"wxpay/internal/model"
)

// 签约联系人信息
type AliSignContactInfoDTO struct {
	ContactName   string
	ContactMobile string
	ContactEmail  string
}

// 签约信息
type AliFaceToFaceSignInfoDTO struct {
	// 事务编号
	BatchNo string
	// 经营类目编码
	MccCode string
	// 特殊资质图片地址
	SpecialLicensePic string
	// 服务费率 （%）
	Rate        string
	SignAndAuth bool
	// 营业执照号码
	BusinessLicenseNo string
	// 营业执照图片地址
	BusinessLicensePic string
	// 营业执照授权函图片地址
	BusinessLicenseAuthPic string
	// 是否长期有效
	LongTerm bool
	// 营业期限
	DateLimitation string
	// 店铺内景图片地址
	ShopScenePic string
	// 店铺门头照片地址
	ShopSignBoardPic string
}

// AliSignAuditDetail 审核详情
type AliSignAuditDetailDTO struct {
	OrderStatus  string
	ConfirmUrl   string
	AgentAppID   string
	RejectReason string
	MerchantPID  string
}

type AlipayAgentSignDTO struct {
	// 申请id
	ID int64
	// 申请单号
	ApplyNo string
	// 品牌id
	BrandID string
	// 服务商 id
	AppID string
	// 阿里商户号
	AlipayAccount string
	// 联系信息
	ContactInfo *AliSignContactInfoDTO
	// 申请事务id
	BatchNo string
	// 申请信息
	SignInfo *AliFaceToFaceSignInfoDTO
	// 签约类型
	SignType string
	// 申请状态
	ApplyState string
	// 申请状态描述
	ApplyStateDesc string
	// 审核详情
	AuditDetail *AliSignAuditDetailDTO
	// 确认签约链接
	ConfirmUrl string
	// 申请人id
	EmpID string
	// 业务方申请单号
	BizApplyNo string
	CreateTime time.Time
	UpdateTime time.Time
}

func ConvertAliSignContactInfo(m *model.AliSignContactInfo) *AliSignContactInfoDTO {
	if m == nil {
		return nil
	}
	return &AliSignContactInfoDTO{
		ContactName:   m.ContactName,
		ContactMobile: m.ContactMobile,
		ContactEmail:  m.ContactEmail,
	}
}

func ConvertAliFaceToFaceSignInfo(m *model.AliFaceToFaceSignInfo) *AliFaceToFaceSignInfoDTO {
	if m == nil {
		return nil
	}
	return &AliFaceToFaceSignInfoDTO{
		MccCode:                m.MccCode,
		SpecialLicensePic:      m.SpecialLicensePic,
		Rate:                   m.Rate,
		SignAndAuth:            m.SignAndAuth,
		BusinessLicenseNo:      m.BusinessLicenseNo,
		BusinessLicensePic:     m.BusinessLicensePic,
		BusinessLicenseAuthPic: m.BusinessLicenseAuthPic,
		LongTerm:               m.LongTerm,
		DateLimitation:         m.DateLimitation,
		ShopScenePic:           m.ShopScenePic,
		ShopSignBoardPic:       m.ShopSignBoardPic,
	}
}

func ConvertAliSignAuditDetailDTO(m *model.AliSignAuditDetail) *AliSignAuditDetailDTO {
	if m == nil {
		return nil
	}
	return &AliSignAuditDetailDTO{
		OrderStatus:  m.OrderStatus,
		ConfirmUrl:   m.ConfirmUrl,
		AgentAppID:   m.AgentAppID,
		RejectReason: m.RejectReason,
		MerchantPID:  m.MerchantPID,
	}
}

func ConvertAliFaceToFaceSignDTO(m *model.AlipayAgentSign) *AlipayAgentSignDTO {
	if m == nil {
		return nil
	}
	cInfo := &model.AliSignContactInfo{}
	cInfo.ToStruct(m.ContactInfo)
	sInfo := &model.AliFaceToFaceSignInfo{}
	sInfo.ToStruct(m.SignInfo)
	aInfo := &model.AliSignAuditDetail{}
	aInfo.ToStruct(m.AuditDetail)
	return &AlipayAgentSignDTO{
		ID:             m.ID,
		ApplyNo:        m.ApplyNo,
		BrandID:        m.BrandID,
		AppID:          m.AppId,
		AlipayAccount:  m.AlipayAccount,
		ContactInfo:    ConvertAliSignContactInfo(cInfo),
		BatchNo:        m.BatchNo,
		SignInfo:       ConvertAliFaceToFaceSignInfo(sInfo),
		SignType:       m.SignType,
		ApplyState:     m.ApplyState.String,
		ApplyStateDesc: m.ApplyStateDesc.String,
		AuditDetail:    ConvertAliSignAuditDetailDTO(aInfo),
		ConfirmUrl:     m.ConfirmURL.String,
		BizApplyNo:     m.BizApplyNo.String,
		EmpID:          m.EmpID.String,
		CreateTime:     m.CreateTime,
		UpdateTime:     m.UpdateTime,
	}
}

func AdaptAliSignContact(d *AliSignContactInfoDTO) *model.AliSignContactInfo {
	if d == nil {
		return nil
	}
	return &model.AliSignContactInfo{
		ContactName:   d.ContactName,
		ContactMobile: d.ContactMobile,
		ContactEmail:  d.ContactEmail,
	}
}

func AdaptAliFaceToFaceSignInfo(d *AliFaceToFaceSignInfoDTO) *model.AliFaceToFaceSignInfo {
	if d == nil {
		return nil
	}
	return &model.AliFaceToFaceSignInfo{
		MccCode:                d.MccCode,
		SpecialLicensePic:      d.SpecialLicensePic,
		Rate:                   d.Rate,
		SignAndAuth:            d.SignAndAuth,
		BusinessLicenseNo:      d.BusinessLicenseNo,
		BusinessLicensePic:     d.BusinessLicensePic,
		BusinessLicenseAuthPic: d.BusinessLicenseAuthPic,
		LongTerm:               d.LongTerm,
		DateLimitation:         d.DateLimitation,
		ShopScenePic:           d.ShopScenePic,
		ShopSignBoardPic:       d.ShopSignBoardPic,
	}
}

func AdaptAliSignAuditDetail(d *AliSignAuditDetailDTO) *model.AliSignAuditDetail {
	if d == nil {
		return nil
	}
	return &model.AliSignAuditDetail{
		AgentAppID:   d.AgentAppID,
		RejectReason: d.RejectReason,
		MerchantPID:  d.MerchantPID,
		OrderStatus:  d.OrderStatus,
		ConfirmUrl:   d.ConfirmUrl,
	}
}

func AdaptAliFaceToFaceSign(d *AlipayAgentSignDTO) *model.AlipayAgentSign {
	if d == nil {
		return nil
	}
	return &model.AlipayAgentSign{
		ID:             d.ID,
		ApplyNo:        d.ApplyNo,
		BrandID:        d.BrandID,
		AppId:          d.AppID,
		AlipayAccount:  d.AlipayAccount,
		ContactInfo:    AdaptAliSignContact(d.ContactInfo).ToJson(),
		BatchNo:        d.BatchNo,
		SignInfo:       AdaptAliFaceToFaceSignInfo(d.SignInfo).ToJson(),
		SignType:       d.SignType,
		ApplyState:     util.AdaptNullString(d.ApplyState),
		ApplyStateDesc: util.AdaptNullString(d.ApplyStateDesc),
		AuditDetail:    AdaptAliSignAuditDetail(d.AuditDetail).ToJson(),
		ConfirmURL:     util.AdaptNullString(d.ConfirmUrl),
		BizApplyNo:     util.AdaptNullString(d.BizApplyNo),
		EmpID:          util.AdaptNullString(d.EmpID),
		CreateTime:     d.CreateTime,
		UpdateTime:     d.UpdateTime,
	}
}

// 支付宝产品签约状态
type AliSignStatus struct {
	ProductName string
	ProductCode string
	Status      string
	StatusDesc  string
}
